package com.wei.czz.framework.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.vo.common.importTask.ImportTaskFormVo;
import com.wei.czz.framework.common.dao.ImportTaskDao;
import com.wei.czz.framework.common.entity.ImportTaskEntity;
import com.wei.czz.framework.common.service.ImportTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2022-03-19 14:10:11
 * className: ImportTaskServiceImpl 文件导入请求服务接口实现类
 * version: 1.0
 * description:
 */
@Service("importTaskService")
public class ImportTaskServiceImpl extends ServiceImpl<ImportTaskDao, ImportTaskEntity> implements ImportTaskService {

    private static final Logger log = LoggerFactory.getLogger(ImportTaskServiceImpl.class);

    @Override
    public void saveOrEdit(ImportTaskEntity importTask) {
        Long id = importTask.getId();

        if (Objects.isNull(id)) {
            int count = baseMapper.insert(importTask);
            log.info("插入文件导入任务完成。count={}", count);
        } else {

            int count = baseMapper.updateById(importTask);
            log.info("修改文件导入任务完成。count={}", count);
        }
    }

    @Override
    public PageDto<ImportTaskEntity> getPageList(ImportTaskFormVo importTaskFormVo) {

        // mp分页对象
        Page<ImportTaskEntity> page = new Page<>(importTaskFormVo.getPage(), importTaskFormVo.getLimit());

        LambdaQueryWrapper<ImportTaskEntity> importTaskLambdaQueryWrapper = new LambdaQueryWrapper<>();
        importTaskLambdaQueryWrapper.eq(Objects.nonNull(importTaskFormVo.getUserId()),
                ImportTaskEntity::getCreateUserId, importTaskFormVo.getUserId())
                .eq(Objects.nonNull(importTaskFormVo.getType()), ImportTaskEntity::getType, importTaskFormVo.getType())
                .eq(Objects.nonNull(importTaskFormVo.getMode()), ImportTaskEntity::getMode, importTaskFormVo.getMode())
                .eq(Objects.nonNull(importTaskFormVo.getStatus()), ImportTaskEntity::getStatus,
                        importTaskFormVo.getStatus())
                .eq(ImportTaskEntity::getDeleteStatus, importTaskFormVo.getDeleteStatus());
        // 设置排序方式
        importTaskLambdaQueryWrapper.orderByDesc(ImportTaskEntity::getId);
        // 分页查询
        baseMapper.selectPage(page, importTaskLambdaQueryWrapper);
        log.info("分页查询文件导入任务完成。list.size={} totalNum={}", page.getRecords().size(), page.getTotal());

        return new PageDto<>(page);
    }

    @Override
    public ImportTaskEntity get(Long id) {
        ImportTaskEntity importTask = baseMapper.selectById(id);
        if (Objects.isNull(importTask)) {
            log.info("文件导入任务不存在");
            throw new CzzException(ResultEnum.ILLEGAL_FAIL);
        }
        if (CommonEnum.ONE.getValue().equals(importTask.getDeleteStatus())) {
            log.info("文件导入任务已删除");
            throw new CzzException(ResultEnum.REQUEST_FAIL.getCode(), "文件导入任务不存在，请确认后重试");
        }
        return importTask;
    }
}